using System;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Web.SessionState;



namespace DD.ShoppingCart
{
    public static class CartManager
    {
        public static void AddItem(int ProductId, int StyleId, string DropshipId, int Qty)
        {
            Visitors.Visitor vis = new DD.Visitors.Visitor();
            if (vis.IsLoggedIn())
            {
                DD.Data.SQL.CartManager.AddItemToCart(ProductId, StyleId, DropshipId, Qty);
            }
            else
            {
                DD.Data.Session.CartManager.AddItemToCart(ProductId, StyleId, DropshipId, Qty);
            }
            
        }

        public static void ChangeQty(int Uid, int Qty)
        {
            Visitors.Visitor vis = new DD.Visitors.Visitor();
            if (vis.IsLoggedIn())
            {
                DD.Data.SQL.CartManager.UpdateQty(Qty, Uid);
            }
            else
            {
                DD.Data.Session.CartManager.UpdateQty(Qty, Uid);
            }
        }

        public static void RemoveItem(int UniqueId)
        {
            Visitors.Visitor vis = new DD.Visitors.Visitor();
            if (vis.IsLoggedIn())
            {
                DD.Data.SQL.CartManager.UpdateQty(0, UniqueId);
            }
            else
            {
                DD.Data.Session.CartManager.UpdateQty(0, UniqueId);
            }
        }

        public static void AddTempToPermenant()
        {
            if (HttpContext.Current.Session["USERCART"] != null)
            {
                DataSet ds = new DataSet();
                ds = (DataSet)HttpContext.Current.Session["USERCART"];
                Visitors.Visitor vis = new DD.Visitors.Visitor();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int pid = (Int32)dr["PID"];
                    int sid = (Int32)dr["SID"];
                    string did = dr["DROPSHIP_ID"].ToString();
                    int uid = vis.UserId;
                    int qty = (Int32)dr["QTY"];
                    DD.Data.SQL.CartManager.AddItemToCart(pid, sid, did, qty);

                }
            }
        }

        public static DataSet GetCartDS()
        {
            DD.Visitors.Visitor vis = new DD.Visitors.Visitor();

            if (vis.IsLoggedIn())
            {

                return DD.Data.SQL.CartManager.GetCartDS();

            }
            else
            {
                return DD.Data.Session.CartManager.GetCartDS();
            }
        }

        
        public static CartItem[] GetItems()
        {
            DataSet ds = new DataSet();
            ds = GetCartDS();
            int Count = ds.Tables[0].Rows.Count;
            CartItem[] cItems = new CartItem[Count];
            for(int i = 0; i < Count; i++)
            {
                CartItem item = new CartItem();
                DataRow dr = ds.Tables[0].Rows[i];
                item.ProductId = (Int32)dr["PID"];
                item.StyleId = (Int32)dr["SID"];
                item.DropShipId = dr["DROPSHIP_ID"].ToString();
                item.Quantity = (Int32)dr["QTY"];
                cItems[i] = item;
               
            }
            Array.Sort(cItems);
            return cItems;
            
            
            
        }
    }

}